<?php

namespace app\common\model\dynamic;


use addons\xyb\XypHelper;
use think\Model;


class Activity extends Model
{

    // 表名
    protected $name = 'dynamic_activity';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static $grant_method_list = [
        1 => '全部',
        2 => '指定区域',
        3 => '指定用户',
    ];

    public static $grant_status_list = [
        1 => ['color'=>'green','label'=>'进行中'],
        2 => ['color'=>'green','label'=>'定时上架'],
        3 => ['color'=>'gold','label'=>'活动结束'],
        4 => ['color'=>'orange','label'=>'草稿箱'],
    ];
    protected $append = [
        'start_date_text',
        'grant_status_text',
        'create_date',
    ];

    public function getStartDateTextAttr($value, $data){
        if(isset($data['start_time']) && strlen($data['start_time']) <= 11){
            return date('Y-m-d H:i', $data['start_time']) .' ~ '. date('Y-m-d H:i', $data['end_time']);
        } else {
            return '';
        }
    }

    public function getGrantStatusTextAttr($value, $data){
        if(!isset($data['grant_status']) || !$data['grant_status']){
            return '';
        }
        return isset(self::$grant_status_list[ $data['grant_status']]) ? self::$grant_status_list[ $data['grant_status']] : '';
    }

    public function getCreateDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        }else {
            return '';
        }
    }

    public static function getAdminLists(){
        $name = request()->param('name', '');
        $deadline = request()->param('deadline/a');
        $grant_status = request()->param('grant_status');
        $field = '';
        $query = self::field($field);
        if ($name) {
            $query->where('name', 'like', "%{$name}%");
        }

        if ($deadline_arr = XypHelper::stringToArray($deadline)) {
            $query->where('start_time','>',  XypHelper::getTimestamp($deadline_arr[0]))
                ->where('end_time','<',  XypHelper::getTimestamp($deadline_arr[1]));
        }
//        if ($examine_method) {
//            $query->where('examine_method', $examine_method);
//        }
        if ($grant_status) {
            $query->where('grant_status', $grant_status);
        }
        return $query->order('id desc')->paginate(10);
    }


    public static function getActivityInfo($activity_id){
        $field = 'id,start_time,end_time,grant_status,status_start_time,slogan as sub_title,banner,share_title,share_img,examine_method';
        $activity_info = self::field($field)->where('id', $activity_id)->find();
        if(!$activity_info){
            return [];
        }
        return $activity_info;
    }

}